<?php
declare(strict_types=1);

namespace App\Controller;

use App\Common\Response\SystemCode;
use App\Job\WxReplaceMediaJob;
use App\Model\Applet;
use App\Model\AppletCoupon;
use App\Model\WeCom;
use App\Model\AuthRole;
use App\Model\RechargeCover;
use App\Model\Tag;
use App\Model\Company;
use App\Model\DataBackhaulSetting;
use App\Model\DataSpread;
use App\Model\DataSpreadPlatform;
use App\Model\Module;
use App\Model\Playlet;
use App\Model\Actor;
use App\Model\RechargeTemplate;
use App\Service\CommicService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Utils\ApplicationContext;
class BaseController
{

    public static $commonStatus = [
        '0'       => '禁用',
        '1'       => '正常',
        '2'       => '未启用',
        '3'       => '重置密码',
    ];

    public function response($code, $msg = '', $data = '')
    {
        if (is_array($msg)) {
            $msg = implode(' ', array_column($msg, 0));
        }

        return [
            'code'    => $code,
            'data'    => $data,
            'message' => $msg,
        ];
    }


    public function templateData(){

        $data =[
            'name' => '',
            'status' => 1,
            'retention' =>[
                ['name' => '挽留档位','amount' => '0','give_coin' => 0,'status' => true,'type' => 1,'day' => 0,
                    'bgtype' => 'http://ky-dj.oss-accelerate.aliyuncs.com/images/36/36b48cb7205c449fc4ca8995b8e18f04.png','classtext' => '','classcolor' => '','classbgc' => '','foottext' => '',
                    'footcolor' => '', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '','giftcolor' => '','amountcolor'=>''
                ],
            ],
            'convention' =>[
                ['name' => '常规档位','amount' => '0','give_coin' => 0,'status' => true,'type' => 1,'day' => 0,
                    'bgtype' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/96/96d40fd18331699c1120f5c9440cbbc4.png','classtext' => '','classcolor' => '#ffffff','classbgc' => '#f3dac0,#eecbd1,#e6b699','foottext' => '',
                    'footcolor' => '#f3dac0,#eecbd1,#e6b699', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '#ff4018','giftcolor' => '#603013','amountcolor'=>'#603013'
                ],
                ['name' => '常规档位','amount' => '0','give_coin' => 0,'status' => true,'type' => 1,'day' => 0,
                    'bgtype' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/96/96d40fd18331699c1120f5c9440cbbc4.png','classtext' => '','classcolor' => '#ffffff','classbgc' => '#f3dac0,#eecbd1,#e6b699','foottext' => '',
                    'footcolor' => '#f3dac0,#eecbd1,#e6b699', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '#ff4018','giftcolor' => '#603013','amountcolor'=>'#603013'
                ],
                ['name' => '常规档位','amount' => '0','give_coin' => 0,'status' => true,'type' => 1,'day' => 0,
                    'bgtype' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/96/96d40fd18331699c1120f5c9440cbbc4.png','classtext' => '','classcolor' => '#ffffff','classbgc' => '#f3dac0,#eecbd1,#e6b699','foottext' => '',
                    'footcolor' => '#f3dac0,#eecbd1,#e6b699', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '#ff4018','giftcolor' => '#603013','amountcolor'=>'#603013'
                ],
                ['name' => '常规档位','amount' => '0','give_coin' => 0,'status' => true,'type' => 1,'day' => 0,
                    'bgtype' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/96/96d40fd18331699c1120f5c9440cbbc4.png','classtext' => '','classcolor' => '#ffffff','classbgc' => '#f3dac0,#eecbd1,#e6b699','foottext' => '',
                    'footcolor' => '#f3dac0,#eecbd1,#e6b699', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '#ff4018','giftcolor' => '#603013','amountcolor'=>'#603013'
                ],
            ],
            'member' => [
                ['name' => '1天会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 1,
                    'bgtype' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/c7/c7df7dc0149da020cf8735da5a30d60f.png','classtext' => '','classcolor' => '#ffffff','classbgc' => '#eb190d,#eb190d,#eb190d','foottext' => '',
                    'footcolor' => '#ffffff,#ffffff,#ffffff', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '#ffffff','giftcolor' => '#603013','amountcolor'=>'#ffffff'
                ],
                ['name' => '3天会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 3,
                    'bgtype' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/c7/c7df7dc0149da020cf8735da5a30d60f.png','classtext' => '','classcolor' => '#ffffff','classbgc' => '#eb190d,#eb190d,#eb190d','foottext' => '',
                    'footcolor' => '#ffffff,#ffffff,#ffffff', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '#ffffff','giftcolor' => '#603013','amountcolor'=>'#ffffff'
                ],
                ['name' => '7天会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 7,
                    'bgtype' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/c7/c7df7dc0149da020cf8735da5a30d60f.png','classtext' => '','classcolor' => '#ffffff','classbgc' => '#eb190d,#eb190d,#eb190d','foottext' => '',
                    'footcolor' => '#ffffff,#ffffff,#ffffff', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '#ffffff','giftcolor' => '#603013','amountcolor'=>'#ffffff'
                ],
                ['name' => '15天会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 15,
                    'bgtype' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/c7/c7df7dc0149da020cf8735da5a30d60f.png','classtext' => '','classcolor' => '#ffffff','classbgc' => '#eb190d,#eb190d,#eb190d','foottext' => '',
                    'footcolor' => '#ffffff,#ffffff,#ffffff', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '#ffffff','giftcolor' => '#603013','amountcolor'=>'#ffffff'
                ],
                ['name' => '月度会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 31,
                    'bgtype' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/c7/c7df7dc0149da020cf8735da5a30d60f.png','classtext' => '','classcolor' => '#ffffff','classbgc' => '#eb190d,#eb190d,#eb190d','foottext' => '',
                    'footcolor' => '#ffffff,#ffffff,#ffffff', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '#ffffff','giftcolor' => '#603013','amountcolor'=>'#ffffff'
                ],
                ['name' => '季度会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 90,
                    'bgtype' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/c7/c7df7dc0149da020cf8735da5a30d60f.png','classtext' => '','classcolor' => '#ffffff','classbgc' => '#eb190d,#eb190d,#eb190d','foottext' => '',
                    'footcolor' => '#ffffff,#ffffff,#ffffff', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '#ffffff','giftcolor' => '#603013','amountcolor'=>'#ffffff'
                ],
                ['name' => '半年会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 180,
                    'bgtype' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/c7/c7df7dc0149da020cf8735da5a30d60f.png','classtext' => '','classcolor' => '#ffffff','classbgc' => '#eb190d,#eb190d,#eb190d','foottext' => '',
                    'footcolor' => '#ffffff,#ffffff,#ffffff', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '#ffffff','giftcolor' => '#603013','amountcolor'=>'#ffffff'
                ],
                ['name' => '年度会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 366,
                    'bgtype' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/c7/c7df7dc0149da020cf8735da5a30d60f.png','classtext' => '','classcolor' => '#ffffff','classbgc' => '#eb190d,#eb190d,#eb190d','foottext' => '',
                    'footcolor' => '#ffffff,#ffffff,#ffffff', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '#ffffff','giftcolor' => '#603013','amountcolor'=>'#ffffff'
                ],
            ],
            'promotion' => [
                ['name' => '整剧购买','amount' => '0','give_coin' => 0,'status' => false,'type' => 2,'day' => 0,
                    'bgtype' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/c4/c47838d1d178c4c79f107c981c2d2133.png','classtext' => '','classcolor' => '#603013','classbgc' => '#edb45f,#f4ca8a,#fce5bf','foottext' => '',
                    'footcolor' => '#edb45f,#f4ca8a,#fce5bf', 'footicon' => 'https://ky-dj.oss-accelerate.aliyuncs.com/images/88/8893b31a7018d21e5ed7a9e0a0126086.png','giftbgc' => '#ffffff','giftcolor' => '#603013','amountcolor'=>'#ffffff'
                ],
            ],
        ];


        return $data;
    }

    public function get_code($code_code=''){
        $code = [
            '10000' => '成功',
            '20001' => 'open_id不能为空！',
            '20002' => '授权失败！',
            '20030' => '当前章节没有购买的！',
            '20040' => '任务ID不能外空！',
            '20041' => '任务不存在',
            '20042' => '当前任务已经领取',
            '20043' => '不满足条件',
            '20050' => '商品ID不能为空！',
            '20060' => '缺少必要参数！',
            '20061' => '余额不足！',
        ];
        return isset($code[$code_code])? $code[$code_code]:'';
    }

    public function get_applet_id(){
        # 获取肚下程序applet_id
        $request = ApplicationContext::getContainer()->get(RequestInterface::class);
        $headers = $request->getHeaders();
//        echo "headers".print_r($headers).PHP_EOL;
        return $headers["applet_id"][0];
    }

    public function get_ip(){
        $request = ApplicationContext::getContainer()->get(RequestInterface::class);
        $headers = $request->getHeaders();
        if(isset($headers['x-forwarded-for'][0]) && !empty($headers['x-forwarded-for'][0])) {
            return $headers['x-forwarded-for'][0];
        } elseif (isset($headers['x-real-ip'][0]) && !empty($headers['x-real-ip'][0])) {
            return $headers['x-real-ip'][0];
        }elseif(isset($headers['remoteip'][0]) && !empty($headers['remoteip'][0])) {
            return $headers['remoteip'][0];
        }
        $serverParams = $request->getServerParams();
        return $serverParams['remote_addr'] ?? '';
    }

    public function get_user_ua(){
        $request = ApplicationContext::getContainer()->get(RequestInterface::class);
        $headers = $request->getHeaders();
        return isset($headers["user-agent"][0]) ? $headers["user-agent"][0] : "";
    }
    public function actor(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Actor::select(['id as value','name as label'])->where(['status' => Actor::STATUS_SHOW])->orderBy('created_at','desc')->get());
    }
    public function company(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Company::select(['id as value','name as label'])->where(['status' => Company::STATUS_SHOW])->get());
    }
    public function module(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Module::select(['id as value','name','label'])->get());
    }
    public function AuthRole(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',AuthRole::select(['id as value','name as label'])->where(['status' => AuthRole::STATUS_SHOW])->get());
    }
    public function applet(int $type = 0){

        if(!empty($type)){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Applet::select(['id as value','name as label'])->where(['type' => $type])->where(['status' => Applet::STATUS_SHOW])->get());
        }else{
            return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Applet::select(['id as value','name as label'])->where(['status' => Applet::STATUS_SHOW])->get());
        }
    }

    public function weCom(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',WeCom::select(['id as value','name as label'])->where(['status' => WeCom::STATUS_SHOW])->get());
    }

    public function playlet(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Playlet::select(['id as value','name as label',db::Raw('IFNULL(`alias_name`,`name`) as alias_name')])->where(['status' => Playlet::STATUS_SHOW])->get());
    }

    public function playletOrdinary(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Playlet::select(['id as value','name as label'])->where(['status' => Playlet::STATUS_SHOW,'playlet_type' => 1])->get());
    }

    public function playletBlocksRanking(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Playlet::select(['id as value','name as label','cover'])
            ->where(['status' => Playlet::STATUS_SHOW])
            ->where(['album_status' => '3','drama_status' => 3,'playlet_type' => 1])
            ->orderBy('id','desc')->get());
    }

    public function templete(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',$this->templateData());
    }

    public function platform(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',DataSpreadPlatform::select(['id as value','name as label'])->get());
    }

    public function backhaulSetting(int $id){


        //查询对应的推广链接
        $dataSpreadInfo  = DataSpread::query()->where(['id' => $id])->first();
        if(empty($dataSpreadInfo)){

            return $this->response(SystemCode::SYSTEM_ERROR, '推广链接不存在');
        }

        $resultData[] = ['value' => 0,'label' => '不回传'];

        $returnData = DataBackhaulSetting::select(['id as value','name as label'])
            ->where(['company_id' =>$dataSpreadInfo->company_id])
            ->get()->toArray();

        if(!empty($returnData)){

            $resultData = array_merge($resultData,$returnData);
        }

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',$resultData);
    }

    public function appletCoupon(int $applet_id){

        $nowTime = date('Y-m-d H:i:s',time());
        //查询对应的推广链接
        $couponInfo  = AppletCoupon::query()->where(['applet_id' => $applet_id])
            ->where('receive_begin_time','<=',$nowTime)
            ->where('receive_end_time','>=',$nowTime)
            ->where('status',1)
            ->select(['id',"receive_begin_time","receive_end_time","name","coupon_meta_id","stock_number","valid_duration"])->get();
        if($couponInfo->isEmpty()){
            return $this->response(SystemCode::SYSTEM_ERROR, '小程序券不存在');
        }


        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',$couponInfo);
    }

    public function templeteList(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',RechargeTemplate::select(['id as value','name as label'])->where(['status' => RechargeTemplate::STATUS_SHOW])->get());
    }

    public function Tag(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Tag::select(['id as value','name as label'])->get());
    }

    public function templateType(){

        $data = [
            ['label' => '小剧场','value' => 1],
            ['label' => '剧情介绍','value' => 2],
            ['label' => '故事投稿','value' => 3],
            ['label' => '剧情墙纸','value' => 4]
        ];

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',$data);
    }


    public function rechangeCover(){

       $data =  RechargeCover::query()->where('type',1)->select('id','cover','type','name')->get();
       $icon = RechargeCover::query()->where('type',2)->select('id','cover','type','name')->first();
       foreach ($data as $datum){

           if($datum['id'] == 1){

               $returnData['bgimg'][] = [
                   'id' =>  $datum['id'],
                   'cover' =>  $datum['cover'],
                   'type' =>  $datum['type'],
                   'name' => $datum['name'],
                   'bgtype' => $datum['cover'],
                   'classcolor' => "#ffffff",
                   'classbgc' => "#ff0000",
                   'footcolor' => "#ffffff,#ffffff,#ffffff",
                   'footicon' => $icon->cover,
                   'giftbgc' => '',
                   'giftcolor' => "",
                   'amountcolor' => "#000000",
               ];
           }

           if($datum['id'] == 2){

                $returnData['bgimg'][] = [
                    'id' =>  $datum['id'],
                    'cover' =>  $datum['cover'],
                    'type' =>  $datum['type'],
                    'name' => $datum['name'],
                    'bgtype' => $datum['cover'],
                    'classcolor' => "#ffffff",
                    'classbgc' => "#f3dac0,#eecbd1,#e6b699",
                    'footcolor' => "#f3dac0,#eecbd1,#e6b699",
                    'footicon' => $icon->cover,
                    'giftbgc' => '#ff4018',
                    'giftcolor' => "#ffffff",
                    'amountcolor' => "#603013",
                ];
           }
           if($datum['id'] == 3){

               $returnData['bgimg'][] = [
                   'id' =>  $datum['id'],
                   'cover' =>  $datum['cover'],
                   'type' =>  $datum['type'],
                   'name' => $datum['name'],
                   'bgtype' => $datum['cover'],
                   'classcolor' => "#ffffff",
                   'classbgc' => "#eb190d,#eb190d,#eb190d",
                   'footcolor' => "#ffffff,#ffffff,#ffffff",
                   'footicon' => $icon->cover,
                   'giftbgc' => '#ffffff',
                   'giftcolor' => "#ff4018",
                   'amountcolor' => "#ffffff",
               ];
           }
           if($datum['id'] == 4){

               $returnData['bgimg'][] = [
                   'id' =>  $datum['id'],
                   'cover' =>  $datum['cover'],
                   'type' =>  $datum['type'],
                   'name' => $datum['name'],
                   'bgtype' => $datum['cover'],
                   'classcolor' => "#603013",
                   'classbgc' => "#edb45f,#f4ca8a,#fce5bf",
                   'footcolor' => "#edb45f,#f4ca8a,#fce5bf",
                   'footicon' => '',
                   'giftbgc' => '#ffffff',
                   'giftcolor' => "#ff4018",
                   'amountcolor' => "#ffffff",
               ];
           }
       }

       return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',$returnData);
    }


    public function jumpType(){

        $data = [
            ['label' => '我的','value' => 1],
            ['label' => '任务','value' => 2],
            ['label' => '排行','value' => 3],
            ['label' => '追剧','value' => 4],
            ['label' => '添加收藏','value' => 5],
            ['label' => '充值','value' => 6],
            ['label' => '充值记录','value' => 7],
            ['label' => '点赞历史','value' => 8],
            ['label' => '联系客服','value' => 9],
            ['label' => '推荐','value' => 10],
        ];

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',$data);
    }

    public function refundReason($type = 1){

        $returnData = array();
        if($type == 1){
            $returnData = [

                ['value' => 0,'label' => '暂无描述'],
                ['value' => 1,'label' => '产品问题，影响使用或效果不佳'],
                ['value' => 2,'label' => '售后问题，无法满足需求'],
                ['value' => 3,'label' => '意愿问题，用户主动退款'],
                ['value' => 4,'label' => '价格问题'],
                ['value' => 5,'label' => '其他原因'],
            ];
        }
        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',$returnData);
    }


    public function appletType(){

        $returnData = array();

        $returnData = [

            ['value' => 1,'label' => '微信小程序'],
            ['value' => 2,'label' => '抖音小程序'],
            ['value' => 3,'label' => '快手小程序'],
            ['value' => 10000,'label' => '客户使用']
        ];

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',$returnData);
    }

}